package cn.tedu.ems.controller;

import cn.tedu.ems.mapper.TeacherMapper;
import cn.tedu.ems.pojo.dto.TeacherDTO;
import cn.tedu.ems.pojo.dto.TeacherUpdateDTO;
import cn.tedu.ems.pojo.vo.TeacherAndCourseListVO;
import cn.tedu.ems.pojo.vo.TeacherDetailVO;
import cn.tedu.ems.pojo.vo.TeacherListVO;
import cn.tedu.ems.service.ITeacherService;
import cn.tedu.ems.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 教师模块的控制器层
 *
 * @Author java@Wqy
 * @Version 0.0.1
 */
@Api(tags = "教师管理模块")
@Slf4j
@Validated
@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private ITeacherService teacherService;

    /**
     * 处理添加教师业务
     *
     * @param teacherDTO 接收传入的教师信息
     */
    @ApiOperation("添加教师")
    @ApiOperationSupport(order = 100)
    @PostMapping("/insert")
    public JsonResult<Void> insert(@RequestBody TeacherDTO teacherDTO) {
        log.debug("开始处理添加教师的请求,参数:{}", teacherDTO);
        teacherService.insert(teacherDTO);
        return JsonResult.ok();
    }

    /**
     * 处理根据id删除教师信息
     */
    @ApiOperation("删除教师")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "教师id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1, message = "删除失败,该id无效!") @PathVariable Long id) {
        log.debug("开始处理根据id删除教师的请求,参数:{}", id);
        teacherService.deleteById(id);
        return JsonResult.ok();
    }


    /**
     * 处理修改教师信息业务
     *
     * @param teacher 修改的教师信息
     */
    @ApiOperation("修改教师")
    @ApiOperationSupport(order = 300)
    @PostMapping("/update")
    public JsonResult<Void> updateByJobId(Long id, @RequestBody TeacherUpdateDTO teacher) {
        log.debug("开始处理根据id[{}]修改教师信息的业务,参数:{}", id, teacher);
        teacherService.updateById(id, teacher);
        return JsonResult.ok();
    }

    /**
     * 处理查询教师详细信息
     *
     * @param name  教师名称
     * @param jobId 职工id
     * @return 返回教师详情
     */
    @ApiOperation("查询详细信息")
    @ApiOperationSupport(order = 600)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "教师名称", required = true, dataType = "string"),
            @ApiImplicitParam(name = "jobId", value = "教师工号", required = true, dataType = "string"),
    })
    @PostMapping("/{name}/{jobId:[0-9]+}/selectDetail")
    public JsonResult<TeacherDetailVO> selectDetail(@PathVariable String name, @PathVariable String jobId) {
        log.debug("开始处理根据教师姓名[{}]和职工id[{}]查询教师信息", name, jobId);
        TeacherDetailVO teacherDetailVO = teacherService.selectDetail(name, jobId);
        return JsonResult.ok(teacherDetailVO);
    }

    /**
     * 根据教师id来查询该教师的详细信息
     *
     * @param id 教师id
     * @return 返回教师id的详情类
     */
    @ApiOperation("根据id查询教师信息")
    @ApiOperationSupport(order = 601)
    @GetMapping("/selectById")
    public JsonResult<TeacherDetailVO> selectByJobId(@Range(min = 1, message = "查询失败,该id无效!") Long id) {
        log.debug("开始处理根据id查询教师信息的请求,参数:{}", id);
        TeacherDetailVO teacherDetailVO = teacherService.selectById(id);
        return JsonResult.ok(teacherDetailVO);
    }


    /**
     * 光标失焦事件
     *
     * @param jobId 职工id
     * @return 返回查询的信息
     */
    @ApiOperation("失去焦点事件")
    @ApiOperationSupport(order = 900)
    @ApiImplicitParam(name = "jobId", value = "教师工号", dataType = "string")
    @PostMapping("/selectToBlur/{jobId:[0-9]+}")
    public int selectToBlur(@PathVariable String jobId) {
        TeacherDetailVO teacherDetailVO = teacherMapper.selectByJobId(jobId);
        if (teacherDetailVO != null) {
            return 2;
        }
        return 1;
    }

    /**
     * 该方法用来查询教师的列表信息
     *
     * @return List
     */
    @ApiOperation("查询教师列表信息")
    @ApiOperationSupport(order = 603)
    @GetMapping("")
    public JsonResult<List<TeacherListVO>> select() {
        log.debug("开始处理查询教师列表的请求...");
        List<TeacherListVO> select = teacherService.select();
        return JsonResult.ok(select);
    }

    /**
     * 处理查询教师与课程关联表的列表授课信息的请求
     *
     * @return 返回列表集合
     */
    @ApiOperation("查询授课列表信息")
    @ApiOperationSupport(order = 604)
    @GetMapping("/TCList")
    public JsonResult<List<TeacherAndCourseListVO>> selectTCList() {
        log.debug("开始处理查询教师与课程关联表的列表授课信息,无参!");
        List<TeacherAndCourseListVO> teacherAndCourseListVOS = teacherService.selectTCList();
        return JsonResult.ok(teacherAndCourseListVOS);
    }

    /**
     * 该方法用来查询教师的列表信息
     *
     * @return List
     */
    @ApiOperation("模糊教师列表信息")
    @ApiOperationSupport(order = 605)
    @GetMapping("/selectWd")
    public JsonResult<List<TeacherListVO>> selectWd(String wd) {
        log.debug("开始处理查询教师列表的请求...");
        List<TeacherListVO> select = teacherService.selectWd(wd);
        return JsonResult.ok(select);
    }

    /**
     * 该方法用来查询教师的列表信息
     *
     * @return List
     */
    @ApiOperation("模糊教师课程关联列表信息")
    @ApiOperationSupport(order = 606)
    @GetMapping("/selectWdToTC")
    public JsonResult<List<TeacherAndCourseListVO>> selectWdToTC(String wd) {
        log.debug("开始处理查询教师列表的请求...");
        List<TeacherAndCourseListVO> teacherAndCourseListVOS = teacherService.selectWdToTC(wd);
        return JsonResult.ok(teacherAndCourseListVOS);
    }
}
